<?php
# Logging in with Google accounts requires setting special identity, so this example shows how to do it.
require 'php/openid.php';
require 'php/database.php';
require_once 'php/role.php';
require_once 'php/functions_phd.php';
require_once 'php/functions_adv.php';
require_once 'php/functions_offfice.php';
try {
    # Change 'localhost' to your domain name.
    $openid = new LightOpenID('14.139.56.179:8085');
    if(!$openid->mode) {
        if(isset($_GET['login'])) {
            $openid->identity = 'https://www.google.com/accounts/o8/id';
			$openid->required = array(
								'namePerson',
								'namePerson/first',
								'namePerson/last',
								'contact/email',
			);
            header('Location: ' . $openid->authUrl());
        }
?>
<!DOCTYPE html>
<html lang="en" style="width:100%;height:100%;">
	<head>
		<meta charset="utf-8" />

		<!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
		Remove this if you use the .htaccess -->
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

		<title>Granted v0.0</title>
		<meta name="description" content="" />
		<meta name="author" content="digvijay" />

		<meta name="viewport" content="width=device-width; initial-scale=1.0" />

		<!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
		<link rel="shortcut icon" href="/favicon.ico" />
		<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
	</head>

	<body style="width:100%;height:100%;">
		
		<div style="position:fixed;top:300px;z-index:1;width:100%;height:200px;background-image: url('../images/shared/granted.png');background-repeat:repeat-x;">
		
		</div>
		<div style="position:absolute;z-index:100;width:100%;height:200px;">
			<header style="z-index:101;width:100%;height:200px;">
				<img style="z-index:102;display:block;margin-top:90px;margin-left:auto;margin-right:auto;" src="../images/login/style3colorsmall.png" height=100 width=500>
				<div style="z-index:103;background-image: url('images/login/login_box.png');width:513px;height:218px;margin-top:20px;margin-left:auto;margin-right:auto;">
					<form action="?login" method="post">
					<input type="image" src="images/login/google-icon_90.png" style="z-index:104;position:relative;left:80px;top:75px;" alt="Submit button">
					</form>
					
				</div>
			</header>
		</div>
	</body>
</html>

<?php
    } elseif($openid->mode == 'cancel') {
        echo 'User has canceled authentication!';
    } else {
        if($openid->validate()){
        	$openid->identity;
			$data = $openid->getAttributes();
			$email = $data['contact/email'];
			//echo $email;
			$db = new DatabaseManager();
			$connection = $db->getDB();
			//DatabaseManager::initDBConnection();
			//$connection = DatabaseManager::getDB();
			echo "after connection";
			//$connection = mysqli_connect("192.168.1.20", "group5", "grp5@leg", "group5") or die ("Unable to connect!");
			$query = "SELECT * FROM registered WHERE email_id='".$email."'";
			//$query = "select * from registered";
			$result = mysqli_query($connection,$query);
			if(mysqli_num_rows($result)>=1){
				$temp = mysqli_fetch_array($result,MYSQLI_ASSOC);
				session_start();
				if($temp['type']=='phd'){
					$p = new phd($temp['id'],$db->getDB());
					$_SESSION['phd']=$p;
					if($temp['register']==1){
						mysqli_free_result($result);
						echo "<meta http-equiv=\"refresh\" content=\"0;URL=phd/home.php\">";
					}
					else if($temp['register']==0){
						
					}
				}
				else if($temp['type']=='advisor'){
					$a = new advisor($temp['id']);
					$_SESSION['adv']=$a;
					if($temp['register']==1){
						mysqli_free_result($result);
						echo "<meta http-equiv=\"refresh\" content=\"0;URL=advisor/home.php\">";
					}
					else if($temp['register']==0){
						
					}
				}
				else if($temp['type']=='pgc'){
					$a = new pgc($temp['id']);
					$_SESSION['pgc']=$a;
					if($temp['register']==1){
						mysqli_free_result($result);
						echo "<meta http-equiv=\"refresh\" content=\"0;URL=pgc/home.php\">";
					}
					else if($temp['register']==0){
						
					}
				}
				else if($temp['type']=='office'){
					$a = new office($temp['id']);
					$_SESSION['office']=$a;
					if($temp['register']==1){
						mysqli_free_result($result);
						echo "<meta http-equiv=\"refresh\" content=\"0;URL=Office/home.php\">";
					}
					else if($temp['register']==0){
						
					}
				}
				/*else if($temp['register']==0 and $temp['type']=="phd"){
						echo "<meta http-equiv=\"refresh\" content=\"0;URL=welcome.html\">";
					}*/
					
			
				else {
						
					mysqli_free_result($result);
					echo "Something went wrong!";
				}
				
    		}
			else{
				mysqli_free_result($result);
				echo "<meta http-equiv=\"refresh\" content=\"0;URL=new_user.php\">";
			}
		}
    }
} catch(ErrorException $e) {
    echo $e->getMessage();
}
